<?php
/*
 * Hooligan - A sports facilities management software
 * Copyright (C) 2009/2010 Gianluca Bargelli <g.bargelli@gmail.com>,
 * Andrea Biocco <abiocco@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Project home: <http://code.google.com/hooligan>
 *
 */
/**
 * Description of Model_Session
 *
 * @author Gianluca
 */
class Model_Session
{
    //La sessione da ritornare
    private $_session;
    //Array con tempo di accesso e tempo di gc
    public $maxTime;
    //Database dove gestire le sessioni
    private $database;

    public function __construct(Model_Db $database)
    {
        $this->database=$database;
        //Tempo di accesso della sessione
        $this->maxTime['access'] = time();
        //Carica parametri della Garbage Collector da php.ini
        $this->maxTime['gc'] = get_cfg_var('session.gc_maxlifetime');
        //Personalizza le funzioni per la gestione delle sessioni
        session_set_save_handler(array($this,'_open'),
                array($this,'_close'),
                array($this,'_read'),
                array($this,'_write'),
                array($this,'_destroy'),
                array($this,'_clean')
                );
        //Registra l'esecuzione di session_write_close in sospensione o in chiusura
        register_shutdown_function('session_write_close');
        //Avvia la sessione
        session_start();

    }

    public function _open()
    {
        return true;
    }

    public function _close()
    {
        $this->_clean($this->maxTime['gc']);
    }

    public function _read($id)
    {
        $getData= $this->database->getConnection()->prepare("SELECT data FROM
                                            Sessions AS Session
                                            WHERE Session.id = ?
                                           ");
        if ( false===$getData ) {
          // now what?
        }

        $getData->bind_param('s',$id);
        $getData->bind_result($data);
        if ( false===$getData->execute() ) {
          // now what?
        }
        return  $getData->fetch() ? $data : '';
    }


    public function _write($id, $data)
    {
        $getData = $this->database->getConnection()->prepare("REPLACE INTO
            Sessions
            VALUES (?, ?, ?)");
        $getData->bind_param('sss', $id, $this->maxTime['access'], $data);

        return $getData->execute();
    }

    public function _destroy($id)
    {
        $getData=$this->database->getConnection()->prepare("DELETE FROM
            Sessions
            WHERE id = ?");
        $getData->bind_param('S', $id);
        return $getData->execute();
    }

    public function _clean($max)
    {
        $old=($this->maxTime['access'] - $max);

        $getData = $this->database->getConnection()->prepare("DELETE FROM Sessions WHERE access < ?");
        $getData->bind_param('s', $old);
        return $getData->execute();
    }

    public function getDatabase()
    {
        return $this->database;
    }
    
    public function setDatabase(DBClass $database)
    {
        $this->database=$database;
    }
}
?>
